/**
 * Created by 宏宾 on 2017/3/12.
 */




var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pid"
        },
        key:{
            url:"xUrl"
        }
    },
    callback:{
        onClick: zTreeOnClick,
        onRightClick: OnRightClick

    }
};

//加载树形数据
function loadTree() {
    $.get("/permission/list",function (data) {
        var  zNodes = data.data;

        treeObj = $.fn.zTree.init($("#rootTree"), setting, zNodes);
        treeObj.expandAll(true);
        //获取根节点
        //我了个去 必须初始化之后去获取对象。
        zTree = $.fn.zTree.getZTreeObj("rootTree");

        //返回一个根节点
        var treeNode = treeObj.getNodesByFilter(function (node) { return node.level == 0 }, true);


        var tr =' <tr class="text-c" >' +
            '<td>'+treeNode.id+'</td>' +
            '<td>'+treeNode.name+'</td>' +
            '<td>'+treeNode.url+'</td>' +
            '<td>'+returnNONull(treeNode.description)+'</td>' +
            '</tr>';
        $(".table tbody").empty().append(tr);
    });
}
/**添加节点权限*/
function add_per() {
    hideRMenu();
    var par_code = zTree.getSelectedNodes()[0];
    if(!par_code){
        layer.alert("请选择节点后进行操作！");
        return false;
    }

    console.info(zTree.getSelectedNodes()[0].name);
    var pid = zTree.getSelectedNodes()[0].id;
    var name = zTree.getSelectedNodes()[0].name;

    layer_show_data("添加权限","/permission/add?pid="+pid+"&name="+name,500,400);
}
/**edit节点权限*/
function edit_per() {
    hideRMenu();
    var par_code = zTree.getSelectedNodes()[0];
    if(!par_code){
        layer.alert("请选择节点后进行操作！");
        return false;
    }
    var snode = zTree.getSelectedNodes()[0];
    if(snode.level == 0){
        layer.alert("根节点禁止编辑");
        return false;
    }

    var id = zTree.getSelectedNodes()[0].id;

    layer_show_data("添加权限","/permission/edit/"+id ,500,400);
}
/**del节点权限*/
function del_per() {
    hideRMenu();
    var par_code = zTree.getSelectedNodes()[0];
    if(!par_code){
        layer.alert("请选择节点后进行操作！");
        return false;
    }

    var snode = zTree.getSelectedNodes()[0];
    if(snode.level == 0){
        layer.alert("无法删除根节点");
        return false;
    }

    if(snode.isParent == true){
        layer.alert("请删除子节点后再进行操作!");
        return false;
    }
    layer.confirm('删除须谨慎，确认要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……

        $.get("/permission/delete/"+snode.id,{ts:new Date().getTime()},function(data){
            if(data==1){
                location.replace(location.href);
            }
        });
    });


}

/**鼠标单击事件*/
function zTreeOnClick(event, treeId, treeNode) {
    var tr =' <tr class="text-c" >' +
        '<td>'+treeNode.id+'</td>' +
        '<td>'+treeNode.name+'</td>' +
        '<td>'+treeNode.url+'</td>' +
        '<td>'+returnNONull(treeNode.description)+'</td>' +
        '</tr>';
    $(".table tbody").empty().append(tr);
}
/**鼠标右键事件*/
function OnRightClick(event, treeId, treeNode) {
    var x = event.clientX>200?event.clientX-200:event.clientX;
    var y = event.clientY-50;
    if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
        console.info("root");
        zTree.cancelSelectedNode();

        showRMenu("root", x, y);
    } else if (treeNode && !treeNode.noR) {

        //noR是节点的属性，控制有无右键菜单，我们也可以自定义其他属性
        // console.info("left : "+x+" top : "+y);
        zTree.selectNode(treeNode);
        showRMenu("node", x, y);
    }
}
/**
 * 显示菜单
 * */
function showRMenu(type, x, y) {
    $("#rMenu ul").show();
    if (type=="root") {
        $("#m_del").hide();
        $("#m_add").hide();
        $("#m_edit").hide();
    } else {
        $("#m_del").show();
        $("#m_add").show();
        $("#m_edit").show();
    }
    $("#rMenu").css({"top":y+"px", "left":x+"px", "visibility":"visible"});

    $("body").bind("mousedown", onBodyMouseDown);
}
/**隐藏菜单*/
function hideRMenu() {
    if ($("#rMenu")) $("#rMenu").css({"visibility": "hidden"});
    $("body").unbind("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event){
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
        $("#rMenu").css({"visibility" : "hidden"});
    }
}

var zTree ;
$(document).ready(function(){
    loadTree();

});